package com.hytc.mymall.activity;


import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hytc.mymall.MainActivity;
import com.hytc.mymall.R;
import com.hytc.mymall.application.ActivityManager;
import com.hytc.mymall.base.BaseFragment;
import com.hytc.mymall.base.BaseFragmentActivity;
import com.hytc.mymall.bean.LoginBean;
import com.hytc.mymall.frgament.ConcessionsFragmemt;
import com.hytc.mymall.frgament.HomePageFragment;
import com.hytc.mymall.frgament.MerchantFragment;
import com.hytc.mymall.frgament.MineFragment;
import com.hytc.mymall.frgament.PublicWelfareConsumptionFragment;
import com.hytc.mymall.frgament.PublicWelfareMerchantFragment;
import com.hytc.mymall.frgament.SalesmanFragment;
import com.hytc.mymall.frgament.TurnoverFragment;
import com.hytc.mymall.utils.BaseTools;

import java.io.Serializable;

/**
 *
 */

public class HomeActivity extends BaseFragmentActivity implements RadioGroup.OnCheckedChangeListener {

    private FragmentManager fm;
    public RadioGroup radioGroup;
    FragmentTabHost tabHost;
    private String tag2;
    private String tag3;
    private int usertype = 1;

    private void initView() {
        (this.tabHost = (FragmentTabHost) this.findViewById(android.R.id.tabhost)).setup((Context) this, this.getSupportFragmentManager(), android.R.id.tabcontent);
        this.tabHost.getTabWidget().setVisibility(View.GONE);
        final RadioButton radioButton = (RadioButton) this.findViewById(R.id.radio_button1);
        final RadioButton radioButton2 = (RadioButton) this.findViewById(R.id.radio_button1);
        String indicator;
        Serializable s;
        String indicator2;
        Serializable s2;
        Drawable drawable;
        Drawable drawable2;
        String text;
        String text2;

        if (this.usertype == 1 || this.usertype == 24 || this.usertype == 25 || this.usertype == 26) {
            this.tag2 = "publicwelfareconsumption";
            indicator = "PublicWelfareConsumption";
            s = PublicWelfareConsumptionFragment.class;
            this.tag3 = "publicwelfaremerchant";
            indicator2 = "PublicWelfareMerchant";
            s2 = PublicWelfareMerchantFragment.class;
            drawable = this.getResources().getDrawable(R.drawable.rb_drawable_gyxf_selector);
            drawable2 = this.getResources().getDrawable(R.drawable.rb_drawable_gysj_selector);
            text = this.getResources().getString(R.string.public_welfare_consumption);
            text2 = this.getResources().getString(R.string.public_welfare_merchant);
        } else if (this.usertype == 2 || this.usertype == 27 || this.usertype == 28 || this.usertype == 29) {
            this.tag2 = "turnover";
            indicator = "Turnover";
            s = TurnoverFragment.class;
            this.tag3 = "concessions";
            indicator2 = "Concessions";
            s2 = ConcessionsFragmemt.class;
            drawable = this.getResources().getDrawable(R.drawable.rb_drawable_yye_selector);
            drawable2 = this.getResources().getDrawable(R.drawable.rb_drawable_rl_selector);
            text = this.getResources().getString(R.string.turnover);
            text2 = this.getResources().getString(R.string.concessions);
        } else {
            this.tag2 = "merchant";
            indicator = "Merchant";
            s = MerchantFragment.class;
            this.tag3 = "salesman";
            indicator2 = "Salesman";
            s2 = SalesmanFragment.class;
            drawable = this.getResources().getDrawable(R.drawable.rb_drawable_sj_selector);
            drawable2 = this.getResources().getDrawable(R.drawable.rb_drawable_ywy_selector);
            text = this.getResources().getString(R.string.merchant);
            text2 = this.getResources().getString(R.string.salesman);
        }
        this.tabHost.addTab(this.tabHost.newTabSpec("homepage").setIndicator( "HomePage"), HomePageFragment.class, null);
        this.tabHost.addTab(this.tabHost.newTabSpec(this.tag2).setIndicator( indicator), (Class<?>) s, null);
        this.tabHost.addTab(this.tabHost.newTabSpec(this.tag3).setIndicator( indicator2), (Class<?>) s2, null);
        this.tabHost.addTab(this.tabHost.newTabSpec("mine").setIndicator( "Mine"), MineFragment.class, null);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        radioButton.setCompoundDrawables( null, drawable, null,  null);
        radioButton.setCompoundDrawablePadding(BaseTools.dip2px( this, 2.0f));
        radioButton.setText( text);
        drawable2.setBounds(0, 0, drawable2.getMinimumWidth(), drawable2.getMinimumHeight());
        radioButton2.setCompoundDrawables( null, drawable2,  null,  null);
        radioButton2.setCompoundDrawablePadding(BaseTools.dip2px( this, 2.0f));
        radioButton2.setText( text2);
        final int intExtra = this.getIntent().getIntExtra("selection", 3);
        String currentTabByTag = "mine";
        int n = R.id.radio_button3;
        if (intExtra == 1) {
            currentTabByTag = this.tag2;
            n = R.id.radio_button1;
        } else if (intExtra == 2) {
            currentTabByTag = this.tag3;
            n = R.id.radio_button2;
        } else if (intExtra == 3) {
            currentTabByTag = "mine";
            n = R.id.radio_button3;
        }
        this.tabHost.setCurrentTabByTag(currentTabByTag);
        ((RadioButton) this.findViewById(n)).setChecked(true);
        (this.radioGroup = (RadioGroup) this.findViewById(R.id.main_tab)).setOnCheckedChangeListener(this);
    }


    private void setCurrentTab(final Fragment fragment, final String currentTabByTag, final FragmentTransaction fragmentTransaction) {
//        if (ActivityManager.loginBean != null) {
            if (fragment == null) {
                fragmentTransaction.show(fragment);
            } else {
                fragmentTransaction.attach(fragment);
            }
            this.tabHost.setCurrentTabByTag(currentTabByTag);
            return;
//        }
//        this.startActivity(new Intent((Context) this, (Class) LoginActivity.class));
    }

    public Fragment fingFragment(final String s) {
        return this.fm.findFragmentByTag(s);
    }

    public void onCheckedChanged(final RadioGroup radioGroup, final int n) {
        this.fm = this.getSupportFragmentManager();
        final HomePageFragment homePageFragment = (HomePageFragment) this.fm.findFragmentByTag("homepage");
        BaseFragment baseFragment;
        Fragment fragment;
        if (this.usertype == 1 || this.usertype == 24 || this.usertype == 25 || this.usertype == 26) {
            baseFragment = (PublicWelfareConsumptionFragment) this.fm.findFragmentByTag(this.tag2);
            fragment = this.fm.findFragmentByTag(this.tag3);
        } else if (this.usertype == 2 || this.usertype == 27 || this.usertype == 28 || this.usertype == 29) {
            baseFragment = (TurnoverFragment) this.fm.findFragmentByTag(this.tag2);
            fragment = this.fm.findFragmentByTag(this.tag3);
        } else {
            baseFragment = (MerchantFragment) this.fm.findFragmentByTag(this.tag2);
            fragment = this.fm.findFragmentByTag(this.tag3);
        }
        final MineFragment mineFragment = (MineFragment) this.fm.findFragmentByTag("mine");
        final FragmentTransaction beginTransaction = this.fm.beginTransaction();
        if (homePageFragment != null) {
            beginTransaction.detach(homePageFragment);
        }
        if (baseFragment != null) {
            beginTransaction.detach(baseFragment);
        }
        if (fragment != null) {
            beginTransaction.detach(fragment);
        }
        if (mineFragment != null) {
            beginTransaction.detach(mineFragment);
        }
        switch (n) {
            case R.id.radio_button0: {
                final Intent intent = new Intent((Context) this, (Class) MainActivity.class);
                intent.putExtra("fromehomeactivity", true);
                this.startActivity(intent);
            }
            break;
            case R.id.radio_button1: {
                this.setCurrentTab(baseFragment, this.tag2, beginTransaction);
            }
            break;
            case R.id.radio_button2: {
                this.setCurrentTab(fragment, this.tag3, beginTransaction);
            }
            break;
            case R.id.radio_button3: {
                this.setCurrentTab(mineFragment, "mine", beginTransaction);
            }
            break;
        }
    }

    @Override
    protected void onCreate(final Bundle bundle) {
        super.onCreate(bundle);
        this.setContentView(R.layout.activity_home);
        final LoginBean.Data_Login loginBean = ActivityManager.loginBean;
        if (loginBean != null && Integer.valueOf(loginBean.usertype) != null) {
            this.usertype = loginBean.usertype;
        } else {
            this.usertype = 1;
        }
        this.usertype = 1;
        this.initView();
    }
}

